package com.hongtong.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hongtong.auto.JSONAuthentication;
import com.hongtong.entity.TeCur;
import com.hongtong.entity.TeQa;
import com.hongtong.mapper.TeCurMapper;
import com.hongtong.mapper.TeQaMapper;
import com.hongtong.mapper.TeUserMapper;
import com.hongtong.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hongtong
 * @since 2022-04-13
 */
@RestController
@RequestMapping("/te-qa")
public class TeQaController extends JSONAuthentication {
    @Autowired
    TeQaMapper teQaMapper;
    @Autowired
    TeCurMapper teCurMapper;
    @Autowired
    TeUserMapper teUserMapper;
    @ApiOperation(value = "学生添加对此堂课程的疑问")
    @PostMapping("/addQu")
    public void addCar(HttpServletRequest request, HttpServletResponse response, @RequestParam("problem") String problem,@RequestParam("curId") int curId,@RequestParam("userId")int userId) throws  Exception{
        TeQa teQa=new TeQa();
        teQa.setProblem(problem);
        teQa.setCurId(curId);
        teQa.setUserId(userId);
            if(teQaMapper.insert(teQa)>0){
                Result ok = Result.ok();
                this.WriteJSON(request, response, ok);
            }else{
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }
        }

    @ApiOperation(value = "老师添加对此问题的解答")
    @PostMapping("/addQuHd")
    public void addQuHd(HttpServletRequest request, HttpServletResponse response, @RequestParam("answer") String answer,@RequestParam("userId") int userId) throws  Exception{
        TeQa teQa=new TeQa();
        teQa.setAnswer(answer);
        if(teQaMapper.insert(teQa)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "老师删除课程的疑问")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id ) throws Exception{
        int i=teQaMapper.deleteById(id);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "学生修改对此课堂问题的问题")
    @PostMapping("/upd")
    public void upd(HttpServletRequest request, HttpServletResponse response, @RequestParam("problem") String problem,@RequestParam("id") int id) throws  Exception{
        TeQa teQa = teQaMapper.selectById(id);
        teQa.setProblem(problem);
        if(teQaMapper.updateById(teQa)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找所有问题")
    @PostMapping("/findAll")
    public void findAll(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        Page<TeQa> shUserPage=new Page<>(current,size);
        Page<TeQa> shUserPages=teQaMapper.selectPage(shUserPage,null);
        List<TeQa> records = shUserPages.getRecords();
        int total=teCurMapper.selectCount(null);
        for (TeQa a:records){
            a.setTeUser(teUserMapper.selectById(a.getUserId()));
            a.setTeCur(teCurMapper.selectById(a.getCurId()));
        }
        if(records.size()>0){
            Result ok = Result.ok().data("ques",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "根据课程名查找问题")
    @PostMapping("/findAllByName")
    public void findAllByName(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current,@RequestParam("name")String name)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("name",name);
        TeCur teCur = teCurMapper.selectOne(wrapper);
        if(teCur!=null){
            QueryWrapper wrapper1=new QueryWrapper();
            wrapper1.eq("cur_id",teCur.getId());
            Page<TeQa> shUserPage=new Page<>(current,size);
            Page<TeQa> shUserPages=teQaMapper.selectPage(shUserPage,wrapper1);
            List<TeQa> records = shUserPages.getRecords();
            int total=teCurMapper.selectCount(wrapper1);
            for (TeQa a:records){
                a.setTeUser(teUserMapper.selectById(a.getUserId()));
                a.setTeCur(teCurMapper.selectById(a.getCurId()));
            }
            if(records.size()>0){
                Result ok = Result.ok().data("ques",records).data("total",total);
                this.WriteJSON(request, response, ok);
            } else {
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }
        }else{
            Result error = Result.error().message("无该课程");
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "根据学生id查找他们提出的问题")
    @PostMapping("/findAllByNameHd")
    public void findAllByNameHd(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current,@RequestParam("userId")int userId)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",userId);
        Page<TeQa> shUserPage=new Page<>(current,size);
        Page<TeQa> shUserPages=teQaMapper.selectPage(shUserPage,wrapper);
        List<TeQa> records = shUserPages.getRecords();
        int total=teCurMapper.selectCount(wrapper);
        for (TeQa a:records){
            a.setTeUser(teUserMapper.selectById(a.getUserId()));
            a.setTeCur(teCurMapper.selectById(a.getCurId()));
        }
        if(records.size()>0){
            Result ok = Result.ok().data("ques",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "查找问题根据问题id")
    @PostMapping("/findAllById")
    public void findAllById(HttpServletRequest request, HttpServletResponse response,@RequestParam("id") int id)throws  Exception{
        TeQa teCur = teQaMapper.selectById(id);
        teCur.setTeUser(teUserMapper.selectById(teCur.getUserId()));
        teCur.setTeCur(teCurMapper.selectById(teCur.getCurId()));
        if(teCur!=null){
            Result ok = Result.ok().data("ques",teCur);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }


}

